package pure.engine.model 
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormatAlign;
	import flash.text.TextFormat;
	import pure.engine.core.ns_despair;
	
	use namespace ns_despair;
	

	/**
	 * 文本渲染器
	 * [Author] - PURE
	 * 
	 * new GlowFilter(0,0.8,2,2,6)
	 * new DropShadowFilter(1, 45, 0, 1, 5, 5, 100, 1)
	 */
public class TextRenderer extends Renderer implements IText
{
	/**
	 * [Param] - text     文本内容
	 * [Param] - offsetX  偏移值X
	 * [Param] - offsetY  偏移值Y
	 * [Param] - gapX     内边距X，存在背景有效
	 * [Param] - gapY     内边距Y，存在背景有效
	 */
	public function TextRenderer(text:String, offsetX:Number, offsetY:Number, gapX:Number, gapY:Number) 
	{
		super();
		
		//m_textField.antiAliasType = AntiAliasType.ADVANCED;
		//m_textField.gridFitType = GridFitType.PIXEL;
		//m_textField.sharpness = -244;
		m_textField.autoSize  =  'left';
		m_offsetX             =  offsetX;
		m_offsetY             =  offsetY;
		m_gapX                =  gapX;
		m_gapY                =  gapY;
		this.text             =  text;
	}
	
	
	
	//======================
	// Const
	//======================
	
	
	ns_despair static const t_bgDirtyFlag:uint  =  0x01;
	ns_despair static const t_bgFlag:uint       =  0x02;
	
	
	
	//======================
	// Property
	//======================

	
	/**
	 * 大小
	 */
	public function get size():int
	{ 
		return int(m_textFormat.size); 
	}
	
	public function set size(v:int):void 
	{
		if (int(m_textFormat.size) != v)
		{
			m_textFormat.size = v;
			this._setRendererFlag(r_dirtyRangeFlag);
		}
	}
	
	
	/**
	 * 颜色
	 */
	public function get color():uint
	{
		return uint(m_textFormat.color); 
	}
	
	public function set color(v:uint):void 
	{
		if (uint(m_textFormat.color) != v)
		{
			m_textFormat.color = v;
			this._setRendererFlag(r_dirtyImageFlag);
		}
	}
	
	
	/**
	 * 粗体
	 */
	public function get bold():Boolean 
	{ 
		return Boolean(m_textFormat.bold);
	}
	
	public function set bold(b:Boolean):void 
	{
		if (Boolean(m_textFormat.bold) != b)
		{
			m_textFormat.bold = b;
			this._setRendererFlag(r_dirtyRangeFlag);
		}
	}
	
	
	/**
	 * 字体
	 */
	public function get font():String
	{ 
		return m_textFormat.font;
	}
	
	public function set font(v:String):void 
	{
		if (m_textFormat.font != v)
		{
			m_textFormat.font = v;
			this._setRendererFlag(r_dirtyRangeFlag);
		}
	}
	
	
	/**
	 * 文本
	 */
	public function get text():String
	{ 
		return m_textField.text;
	}
	
	public function set text(v:String):void
	{
		if (m_textField.text != v)
		{
			m_textField.text = v;
			this._setRendererFlag(r_dirtyRangeFlag);
		}
	}

	
	/**
	 * 文本滤镜
	 */
	public function get filters():Array 
	{ 
		return m_textField.filters;
	}
	
	public function set filters(value:Array):void
	{
		m_textField.filters = value;
		this._setRendererFlag(r_dirtyRangeFlag);
	}
	
	

	
	/**
	 * 设置背景
	 * 
	 * [Param] - fillColor      填充颜色
	 * [Param] - fillAlpha      填充透明度
	 * [Param] - ellipse        边角内径
	 * [Param] - lineThickness  边线粗细
	 * [Param] - lineColor      边线颜色
	 * [Param] - lineAlpha      边线透明度
	 */
	public function setBackground(fillColor:uint,
								  fillAlpha:Number = 0.5, 
								  ellipse:Number = 0,
								  lineThickness:Number = 0,
								  lineColor:uint = 0,
								  lineAlpha:Number = 1):void
	{
		m_fillColor      =  fillColor;
		m_fillAlpha      =  fillAlpha;
		m_ellipse        =  ellipse;
		m_lineThickness  =  lineThickness;
		m_lineColor      =  lineColor;
		m_lineAlpha      =  lineAlpha;
		m_txtFlags       =  (t_bgFlag | t_bgDirtyFlag);
		this._setRendererFlag(r_dirtyRangeFlag);
	}
	
	
	
	/* ----------------------------------------------------------------------------------------------------------------
	 * ★★★★★★★★★★★★★★★★★★★★★  private  ★★★★★★★★★★★★★★★★★★★★★
	 * ----------------------------------------------------------------------------------------------------------------
	 */
	
	

	/**
	 * [Overwrite]
	 * 
	 * 画像
	 */
	override ns_despair function _generatePrimaryImage():BitmapData
	{
		var w:Number, h:Number;
		
		m_textField.setTextFormat(m_textFormat);
		
		if (Boolean(m_txtFlags & t_bgFlag))
		{
			w  =  m_textField.width  + (m_gapX << 1);
			h  =  m_textField.height + (m_gapY << 1);
		}
		else
		{
			w  =  m_textField.width;
			h  =  m_textField.height;
		}
		
		if (w != cachedWidth || h != cachedHeight)
		{
			if (cachedImage) 
			{
				cachedImage.dispose();
			}
			
			cachedWidth   =  w;
			cachedHeight  =  h;
			cachedImage   =  new BitmapData(w, h, true, 0x0);
			m_x           =  m_offsetX - w * 0.5;
			m_y           =  m_offsetY - h * 0.5;
		}
		else
		{
			cachedImage.fillRect(cachedImage.rect, 0x0);
		}
		
		if (Boolean(m_txtFlags & t_bgFlag))
		{
			if (!m_container)
			{
				m_container = new Sprite();
				m_container.addChild(m_textField);
				m_textField.x = m_gapX;
				m_textField.y = m_gapY; // - cachedHeight / 30;
			}
			if (Boolean(m_txtFlags & t_bgDirtyFlag))
			{
				m_container.graphics.clear();
				m_container.graphics.beginFill(m_fillColor, m_fillAlpha);
				if (m_lineThickness > 0)
				{
					m_container.graphics.lineStyle(m_lineThickness, m_lineColor, m_lineAlpha);
				}
				m_container.graphics.drawRoundRect(m_lineThickness * 0.5, m_lineThickness * 0.5, cachedWidth - m_lineThickness, cachedHeight - m_lineThickness, m_ellipse, m_ellipse);
				m_txtFlags &= ~t_bgDirtyFlag;
			}
			cachedImage.draw(m_container);
		}
		else
		{
			cachedImage.draw(m_textField);
		}
		
		return cachedImage;
	}
	
	
	/**
	 * [Overwrite]
	 * 
	 * 毁灭
	 */
	override ns_despair function destroy():void
	{
		if (cachedImage)
		{
			cachedImage.dispose();
			cachedImage = null;
		}
		if (m_container)
		{
			m_container.removeChild(m_textField);
			m_container = null;
		}
		m_textField   =  null;
		m_textFormat  =  null;
		
		super.destroy();
	}
	
	
	
	
	
	
	//======================
	// Member
	//======================


	ns_despair var m_textField:TextField = new TextField();
	
	ns_despair var m_textFormat:TextFormat = new TextFormat();
	
	ns_despair var m_container:Sprite;  // 文本背景容器

	ns_despair var cachedImage:BitmapData;
		
	ns_despair var m_txtFlags:int;
	
	ns_despair var cachedWidth:Number;
	
	ns_despair var cachedHeight:Number;
	
	ns_despair var m_offsetX:Number;
	
	ns_despair var m_offsetY:Number;
	
	ns_despair var m_gapX:Number;
	
	ns_despair var m_gapY:Number;
	
	ns_despair var m_fillAlpha:Number;
	
	ns_despair var m_ellipse:Number;
	
	ns_despair var m_lineThickness:Number;
	
	ns_despair var m_lineAlpha:Number;

	ns_despair var m_fillColor:uint;
	
	ns_despair var m_lineColor:uint;

}
}